package com.example.data.permission.mp.mate;

import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.function.Consumer;

/**
 * Created by TD on 2021/7/28
 */
@Intercepts({@Signature(
        type = ResultSetHandler.class,
        method = "handleResultSets",
        args = {Statement.class}
)})
@Component
public class DictInterceptor implements Interceptor {
    private static final Logger log = LoggerFactory.getLogger(DictInterceptor.class);

    @Autowired
    private IDataDict dataDict;

    @Override
    public Object intercept(Invocation var1) throws NoSuchFieldException, InvocationTargetException, IllegalAccessException {
        DefaultResultSetHandler var2 = (DefaultResultSetHandler)var1.getTarget();
        Field var3 = var2.getClass().getDeclaredField("mappedStatement");
        var3.setAccessible(true);
        MappedStatement var4 = (MappedStatement)var3.get(var2);
        List var5 = (List)var1.proceed();
        if (var5.isEmpty()) {
            return var5;
        } else {
            Configuration var6 = var4.getConfiguration();
            Iterator var7 = var5.iterator();
            var7.hasNext();

            while(var7.hasNext()) {
                Object var8 = var7.next();
                // 使用工具类 获取实体类的FieldSetProperty ，然后设置值
                List<FieldSetProperty> setPropertyList = FieldSetPropertyUtils.get(var8.getClass());
                if ( null==setPropertyList||setPropertyList.size() <=0 ){
                    return var5;
                }
                for (int i = 0; i < setPropertyList.size(); i++) {
                    MetaObject metaObject = var6.newMetaObject(var8);
                    fieldSet(metaObject,setPropertyList.get(i));
                }
            }
            // 返回翻译后的值
            return var5;
        }
    }

    protected void fieldSet(MetaObject var1, FieldSetProperty var2) {
        Object var3 = var1.getValue(var2.getFieldName());
        if (null != var3) {
            String var4 = var2.getFieldName();
            String var5 = String.valueOf(var3);
            if (null != this.dataDict) {
                FieldDict var7 = var2.getFieldDict();
                if (null != var7) {
                    var4 = var7.target();
                    var5 = this.dataDict.getNameByCode(var7, var5);
                }
            }

            var1.setValue(var4, var5);
        }

    }

    @Override
    public Object plugin(Object var1) {
        return var1 instanceof ResultSetHandler ? Plugin.wrap(var1, this) : var1;
    }

    @Override
    public void setProperties(Properties var1) {
    }
}
